package com.sjy.controller.codemultiplex;

import com.sjy.dao.CodeNodeRepository;
import com.sjy.dao.model.AlarmRecord;
import com.sjy.dao.model.CodeNode;
import com.sjy.framework.ApiResult;
import com.sjy.framework.codemultiplex.DynamicEngine;
import com.sjy.util.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class AntiTerroristController {

    @Resource
    private DynamicEngine dynamicEngine;

    @Autowired
    private CodeNodeRepository codeNodeRepository;


    @PostMapping("/antiTerrorist/count")
    @ResponseBody
    public ApiResult count(AlarmRecord alarmRecord){
        alarmRecord.setAlarmClass(3L);
        long start = System.currentTimeMillis();
        String className = "com.sjy.business.service.impl.AlarmRecordServiceImpl";
        List<CodeNode> codeNode = codeNodeRepository.getCodeByClassName(className);


        CodeNode codeNode1 = codeNode.get(0);
        String code = codeNode1.getCodeContent();

        String beanName = SpringContextUtil.beanName(className);
        Object obj = null;
        String methodName = "selectAlarmRecordCount";

        Method[] methods =null;
        if(SpringContextUtil.getBean(beanName)==null){
            Class classInstance =  dynamicEngine.compile(className, code);
            if(Objects.isNull(classInstance)){
                return ApiResult.buildSuccess("class not found!");
            }
             methods = classInstance.getMethods();
            SpringContextUtil.registerSingleton(classInstance);
             obj = SpringContextUtil.getBean(beanName);
        }else{
            obj = SpringContextUtil.getBean(beanName);
             methods = obj.getClass().getMethods();
        }
        Method toBeInvoked = null;
        for(Method method:methods){
            if(methodName.equals(method.getName())){
                toBeInvoked =  method;
            }
        }
        try {
            Object count =  toBeInvoked.invoke(obj ,alarmRecord,null);
            System.out.println("时间消耗:"+(System.currentTimeMillis()-start)+"ms");
            return ApiResult.buildSuccess(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ApiResult.buildSuccess("finished!");
    }


//    @PostMapping("/count")
//    @ResponseBody
//    public Integer count(AlarmRecord alarmRecord){
//        Map<String,Object> map = new HashMap<>();
//        alarmRecord.setAlarmClass(3L);
//        long start = System.currentTimeMillis();
//        String className = "com.sjy.business.service.impl.AlarmRecordServiceImpl";
//        List<CodeNode> codeNode = codeNodeRepository.getCodeByClassName(className);
//        CodeNode codeNode1 = codeNode.get(0);
//        String code = codeNode1.getCodeContent();
//        Class classInstance =  dynamicEngine.compile(className, code);
//        String methodName = "selectAlarmRecordCount";
//        Method[] methods = classInstance.getMethods();
//        Method toBeInvoked = null;
//        for(Method method:methods){
//            if(methodName.equals(method.getName())){
//                toBeInvoked =  method;
//            }
//        }
//        try {
//            Object obj = classInstance.newInstance();
//           Object count =  toBeInvoked.invoke(obj,alarmRecord,null);
//            System.out.println("时间消耗:"+(System.currentTimeMillis()-start)+"ms");
//            return Integer.valueOf(count.toString());
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }


//    @PostMapping("/count")
//    public Integer count(AlarmRecord alarmRecord){
//        Map<String,Object> map = new HashMap<>();
//        alarmRecord.setAlarmClass(3L);
//        long start = System.currentTimeMillis();
//        String className = "com.sjy.business.service.impl.AlarmRecordServiceImpl";
//        List<CodeNode> codeNode = codeNodeRepository.getCodeByClassName(className);
//
//
//        CodeNode codeNode1 = codeNode.get(0);
//        String code = codeNode1.getCodeContent();
//
//        String beanName = SpringContextUtil.beanName(className);
//        Object obj = null;
//        String methodName = "selectAlarmRecordCount";
//
//        Method[] methods =null;
//        if(SpringContextUtil.getBean(beanName)==null){
//            Class classInstance =  dynamicEngine.compile(className, code);
//
//             methods = classInstance.getMethods();
//
//            String classPath = "";
//            //  Class<?> type = ClassLoadUtil.loadClass(classPath, className);
//            SpringContextUtil.registerSingleton(classInstance);
//             obj = SpringContextUtil.getBean(beanName);
//        }else{
//            obj = SpringContextUtil.getBean(beanName);
//             methods = obj.getClass().getMethods();
//        }
//        Method toBeInvoked = null;
//        for(Method method:methods){
//            if(methodName.equals(method.getName())){
//                toBeInvoked =  method;
//            }
//        }
//        try {
//            Object count =  toBeInvoked.invoke(obj ,alarmRecord,null);
//            System.out.println("时间消耗:"+(System.currentTimeMillis()-start)+"ms");
//            return Integer.valueOf(count.toString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
}
